<?php
function print_error( $msg )
	{
		print <<<END
		<tr>
			<td colspan=5><font color=red><b>Error: </b></font>$msg</td>
			<td><font color=red><b>Rejected</b></font></td>
		</tr>

END;
	}

function getHeader( $exc, $data )
{
		// string
	
		$ind = $data['data'];
		if( $exc->sst[unicode][$ind] )
			return convertUnicodeString ($exc->sst['data'][$ind]);
		else
			return $exc->sst['data'][$ind];

}


function convertUnicodeString( $str )
{
	for( $i=0; $i<strlen($str)/2; $i++ )
	{
		$no = $i*2;
		$hi = ord( $str[$no+1] );
		$lo = $str[$no];
		
		if( $hi != 0 )
			continue;
		elseif( ! ctype_alnum( $lo ) )
			continue;
		else
			$result .= $lo;
	}
	
	return $result;
}

function uc2html($str) {
	$ret = '';
	for( $i=0; $i<strlen($str)/2; $i++ ) {
		$charcode = ord($str[$i*2])+256*ord($str[$i*2+1]);
		$ret .= '&#'.$charcode;
	}
	return $ret;
}
function _encodeUTF16($string,$method)
{
	$result = $string;
	switch ($method){
		case 'iconv' :   
			$result = iconv('UTF-16LE','UTF-8', $string);
			break;
		case 'mb' :     
			$result = mb_convert_encoding($string, 'UTF-8', 'UTF-16LE');
		break;
	}
	return $result;
}
function get( $exc, $data )
{
	switch( $data['type'] )
	{
		// string
	case 0:
		$ind = $data['data'];
		if( $exc->sst[unicode][$ind] )
			//return uc2html($exc->sst['data'][$ind]);
			return _encodeUTF16($exc->sst['data'][$ind],'mb');
		else
			return $exc->sst['data'][$ind];

		// integer
	case 1:
		return (integer) $data['data'];

		// float
	case 2:
		return (float) $data['data'];
        case 3:
		return gmdate("m-d-Y",$exc->xls2tstamp($data[data]));

	default:
		return '';
	}
}

function fatal($msg = '') {
	echo '[Fatal error]';
	if( strlen($msg) > 0 )
		echo ": $msg";
	echo "<br>\nScript terminated<br>\n";
	if( $f_opened) @fclose($fh);
	exit();
}

function prepareTableData ( $exc, $ws, $fieldcheck, $fieldname ) {
	
			
	$data = $ws['cell'];
	
	foreach( $data as $i => $row ) { // Output data and prepare SQL instructions
		
				
		if ( $i == 0 && $_POST['useheaders'] )
		continue;
		
		$SQL[$i] = "";
		
		for ( $j = 0; $j <= $ws['max_col']; $j++ ) {
		
			if ( isset($fieldcheck[$j]) ) {
			
								
				$SQL[$i] .= $fieldname[$j];
				$SQL[$i] .= "=\"";
				$SQL[$i] .= addslashes ( get ( $exc, $row[$j] ) );
				$SQL[$i] .= "\"";
				
				$SQL[$i] .= ",";
			}
		
				
		}
		
		$SQL[$i] = rtrim($SQL[$i], ',');
		
		$i++;
	}
	
	return $SQL;	
			
} 
// thuongdd
function checkDestination($key){
	$key_can_have=array('Destination URL','Destination URL:','Destination URL :','destination URL :','destination URL');
	if(in_array($key,$key_can_have)){
		return true;
	}else{
		return false;
	}
}

function checkKeywords($key){
	$key_can_have=array('Keywords','Keywords:','Keywords :','keywords','keywords:');
	if(in_array($key,$key_can_have)){
		return true;
	}else{
		return false;
	}
}
function getAdCopy($data_array, $index){
	$result=array();
	for($i=0;$i<count($data_array); $i++){
		$result[]=$data_array[$i][$index];
	}
	return $result;
}
function getAdKeywords($data_array, $index){
	$result=array();
	for($i=0;$i<count($data_array); $i++){
		$result[]=$data_array[$i][$index];
	}
	return $result;
}
function prepareObjectData2Array($data_object){
	$result_data=array();	
	$campaign=$data_object['campaign_name'];
	$campaign_daily_budget=$data_object['campaign_name'];
	$language='ar;bg;ca;cs;da;de;el;en;es;et;fi;fr;hi;hr;hu;id;is;it;iw;ja;ko;lt;lv;nl;no;pl;pt;ro;ru;sk;sl;sr;sv;th;tl;tr;uk;ur;vi;zh_CN;zh_TW';
	$geo_targeting='AU';
	$proximity_targets='';
	$ad_schedule='[]';
	$ad_group=$data_object['ad_group'];	
	
	// 2 line
	if($campaign==''){
		return;
	}	
	$result_data[]=array($campaign,null,$language,$geo_targeting,$proximity_targets,$ad_schedule,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,
	null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null);
	$result_data[]=array($campaign,null,null,null,null,null,$ad_group,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,
	null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null);
	
	$adcopy_array=$data_object['add_text'];
	foreach($adcopy_array as $item){
		$result_data[]=array($campaign,null,null,null,null,null,$ad_group,null,null,null,null,null,null,null,null,null,$item['headline'],$item['description_line_1'],$item['description_line_2'],$item['display_url'],$item['destination_url'],null,null,null,
	null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null);
	}	
	$keyword_array=$data_object['key_words'];
	foreach($keyword_array as $key=>$value){
		if($value!=null){
			$result_data[]=array($campaign,null,null,null,null,null,$ad_group,null,null,null,null,null,$value,'Broad',null,null,null,null,null,null,null,null,null,null,
	null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null);
		}
	}	
	return $result_data;
} 
function array_to_csv($data) {
		if(!is_array($data)){
			return null;
		} // if
			
		$result = array();
		foreach($data as $value_set) {
			$values = array();

			foreach($value_set as $value) {
				$value = str_replace('"', '""', $value);
					
				if(strpos($value, ',') !== false || strpos($value, '"') !== false || strpos($value, "\n") !== false || strpos($value, "\r") !== false) {
					$values[] = '"' . $value . '"';
				} else {
					$values[] = $value;
				} // if
			} // foreach

			$result[] = implode('|', $values);
		} // foreach		
		return implode("\n", $result);
	} // array_to_csv
?>